(define-module (mail)
  #:use-module (guix gexp)
  #:export (
            creds
            vdoms
            vusers
            %smtpd.conf
            ))


;; to create credentials for now, I need to do the following:
;;  find /gnu/store -name '*encrypt*' | grep opensmtpd
;; /gnu/store/blah/opensmtpd/encrypt 
(define creds
  (plain-file "creds"
              ;; easy password
              ;;"joshua $6$aCjVjpgo34rxEwog$EX1UTwnTIMAzjvJhNQv7iJm4C6Ite.L6t7hTRQtGRNpc/mVf3dQoPDwjQQwXNXyaa/9TTLwlej5rS9guXktMg0"
              "joshua $6$hZ3AtXoh7Onxn7Nq$EMuQcjArfTfJV.hrZUS1ESfPdC08IcluyzanIsT2.jewqCMTA/mKoYXv/LK2Eaj3ujeRnsu80f0VrxdUZ0qZf1"))

(define vdoms
  (plain-file
   "vdoms"
   "gnucode.me
gnu-hurd.com"))

(define vusers
  (plain-file
   "vusers"
   "joshua@gnucode.me  joshua
jbranso@gnucode.me     joshua
postmaster@gnucode.me  joshua"))

;; (service opensmptpd-service-type
;;          (opensmptpd-configuration
;;           (pki
;;            (cert
;;             (name "mail.gnucode.me")
;;             (file "/etc/letsencrypt/live/gnucode.me/fullchain.pem"))
;;            (key
;;             (name "mail.gnucode.me")
;;             (file "/etc/letsencrypt/live/gnucode.me/privkey.pem")))
;;           (tables (list
;;                    ;; fix these weird table values.  What's good syntax?
;;                    (table
;;                     (name "creds")
;;                     (values (list "joshua" "someEncryptedPassword"
;;                                   "root" "someEncreyptedPasswordHash")))
;;                    (table
;;                     (name "vdoms")
;;                     (values (list "gnucode.me"
;;                                   "gnu-hurd.com")))
;;                    (table
;;                     (name "vusers")
;;                     (values (list "joshua@gnucode.me joshua"
;;                                   "jbranso@gnucode.me joshua"
;;                                   "postmaster@gnucode.me joshua")))))
;;           (filters (list
;;                     (filter
;;                      (name "dkimsign")
;;                      (command "filter-dkimsign -d gnucode.me -s 20201004 -k /path/to/key")
;;                      (user "joshua")
;;                      (group "joshua"))))
;;           (listens (list
;;                     (listen
;;                      (on "eth0")
;;                      (tls #t)
;;                      (pki "mail.gnucode.me")
;;                      (filter "dkimsign"))
;;                     (listen
;;                      (on "eth0")
;;                      (port 465)
;;                      (smtps #t)
;;                      (pki "mail.gnucode.me")
;;                      (auth "creds"))
;;                     (listen
;;                      (on "oth0")
;;                      (port "587")
;;                      (tls-require #t)
;;                      (pki "mail.gnucode.me")
;;                      (auth "creds"))))
;;           (actions (list
;;                     (action
;;                      (name "receive")
;;                      (maildir #t)
;;                      (virtual "users"))))))

(define %smtpd.conf
  (mixed-text-file "smtpd.conf"
                   "
# This is the smtpd server system-wide configuration file.
# See smtpd.conf(5) for more information.
# borrowed from the archlinux guix
# https://wiki.archlinux.org/index.php/OpenSMTPD#Simple_OpenSMTPD/mbox_configuration

# My TLS certificate and key
table aliases file:/etc/aliases
pki mail.gnucode.me cert \"/etc/letsencrypt/live/gnucode.me/fullchain.pem\"
pki mail.gnucode.me key \"/etc/letsencrypt/live/gnucode.me/privkey.pem\"

# for now I am NOT using the virtual credentials
table creds \"" creds "\"
table vdoms \"" vdoms "\"
table vusers \"" vusers "\"

# an initial attempt at getting dkim-signing to work
#this doesn't work
# filter \"dkimsign\" proc-exec \"filter-dkimsign -d gnucode.me -s 20201004 -k \
#   /home/joshua/linode-guix-system-configuration/email-ssh-keys/gnucode.me.key\" \
#   user joshua group joshua

#this doesn't work
# filter \"dkimsign\" proc-exec \"filter-dkimsign -d gnucode.me -s 20201004 -k /home/joshua/linode-guix-system-configuration/email-ssh-keys/gnucode.me.key\" user joshua group joshua

# this doesn't work
filter \"dkimsign\" proc-exec \"/gnu/store/z3iz3xp59ql4zbp4w06l0sqxciapg8q3-profile/libexec/opensmtpd/filter-dkimsign -d gnucode.me -s 20201004 -k /home/joshua/linode-guix-system-configuration/email-ssh-keys/gnucode.me.key\" user joshua group joshua

# listen on eth0, fairly explainatory
listen on socket # filter \"dkimsign\"
listen on eth0 tls pki mail.gnucode.me # filter \"dkimsign\"
listen on eth0 port 465 smtps pki mail.gnucode.me auth <creds>
listen on eth0 port 587 tls-require pki mail.gnucode.me auth <creds>

action \"receive\" maildir virtual <vusers>
action \"send\" relay

match from any for domain <vdoms> action \"receive\"

# Julien's last line looks like this instead of mine
# match for any from any auth action send
match for any from any auth action \"send\""))
